package com.briup.srms.sms.service.impl;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.briup.srms.base.bean.BaseUser;
import com.briup.srms.base.bean.BaseUserRole;
import com.briup.srms.base.bean.extend.BaseUserExtend;
import com.briup.srms.base.dao.BaseUserMapper;
import com.briup.srms.base.dao.BaseUserRoleMapper;
import com.briup.srms.base.service.IBaseUserService;
import com.briup.srms.sms.bean.Employee;
import com.briup.srms.sms.bean.EmployeeExample;
import com.briup.srms.sms.bean.extend.EmployeeExtend;
import com.briup.srms.sms.dao.EmployeeMapper;
import com.briup.srms.sms.dao.extend.EmployeeExtendMapper;
import com.briup.srms.sms.service.IEmployeeService;
import com.briup.srms.sms.vm.DirectionEmployees;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;
import org.springframework.util.DigestUtils;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

	@Autowired
	private EmployeeMapper employeeMapper;
	@Autowired
	private EmployeeExtendMapper employeeExtendMapper;
	@Autowired
	private IBaseUserService baseUserService;
	@Autowired
	private BaseUserRoleMapper baseUserRoleMapper;
	@Autowired
	private BaseUserMapper baseUserMapper;

	@Override
	public Employee findByAccountId(@NotNull long id) {
		EmployeeExample example = new EmployeeExample();
		example.createCriteria().andUserIdEqualTo(id);
		List<Employee> list = employeeMapper.selectByExample(example);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public PageVM<EmployeeExtend> pageQuery(int page, int pageSize, Long deptId,String realname,String status,Long directionId) {
		List<EmployeeExtend> list = employeeExtendMapper.query(page, pageSize, deptId,realname,status,directionId);
		long total = employeeExtendMapper.count(deptId,realname,status,directionId);
		return new PageVM<>(page, pageSize, total, list);
	}

	@Override
	@Transactional
	public void deleteById(long id) throws CustomerException{
		Employee employee = employeeMapper.selectByPrimaryKey(id);
		if(employee == null) {
			throw new CustomerException("员工信息不存在");
		}
		// 删除员工信息
		employeeMapper.deleteByPrimaryKey(id);
		// 删除员工关联的账号信息
		if (employee.getUserId() != null) {
			baseUserService.deleteById(employee.getUserId());
		}
	}

	@Override
	public void saveOrUpdate(Employee employee,long roleId) throws CustomerException {
		if(employee.getId() == null) {
			// 保存账号信息
			BaseUser baseUser = new BaseUser();
			baseUser.setAvatar("http://121.199.29.84:8888/group1/M00/00/12/rBD-SV-Td6-Aawn0AACq962TS9c719.jpg");
			baseUser.setUsername(employee.getTelephone());
			baseUser.setPassword("briup@2021");
			baseUser.setStatus("正常");
			baseUserService.saveOrUpdate(baseUser);

			// 维护用户角色关系
			BaseUserRole ur = new BaseUserRole();
			ur.setUserId(baseUser.getId());
			ur.setRoleId(roleId);
			baseUserRoleMapper.insert(ur);

			// 保存员工信息
			employee.setUserId(baseUser.getId());
			employee.setStatus(EmployeeExtend.STATUS_NORMAL);
			employeeMapper.insert(employee);
		} else {
			employeeMapper.updateByPrimaryKey(employee);
		}
	}

	@Override
	public void alter(Employee employee) throws CustomerException {
		employeeMapper.updateByPrimaryKey(employee);
	}

	@Override
	@Transactional
	public void leaveBriup(long employeeId) {
		// 1. 更改员工状态
		Employee employee = employeeMapper.selectByPrimaryKey(employeeId);
		if(employee == null) {
			throw new CustomerException("员工信息不存在");
		}
		employee.setStatus(EmployeeExtend.STATUS_FORBIDDEN);
		employeeMapper.updateByPrimaryKey(employee);
		// 2. 其他操作

		// 3. 禁用账号
		BaseUser user = baseUserMapper.selectByPrimaryKey(employee.getUserId());
		if(user != null) {
			user.setStatus(BaseUserExtend.STATUS_FORBIDDEN);
			baseUserMapper.updateByPrimaryKey(user);
		}
	}

	@Override
	public List<DirectionEmployees> queryDirectionEmployees(Long directionId,String status) {
		return employeeExtendMapper.selectDirectionEmployees(directionId, status);
	}
}
